//
//  ASSession+Profile.m
//  AppleAuth
//
//  Created by 吴双 on 2023/1/23.
//

#import "ASSession+Profile.h"
#import <AltSign/AltSign.h>

@implementation ASTeamSession (Profile)


- (void)fetchProvisioningProfileForAppID:(ALTAppID * _Nonnull)appID
                       completionHandler:(void (^ _Nonnull)(ALTProvisioningProfile * _Nullable provisioningProfile, NSError * _Nullable error))completionHandler {
    [[ALTAppleAPI sharedAPI] fetchProvisioningProfileForAppID:appID
                                                         team:self.team
                                                      session:self.session
                                            completionHandler:completionHandler];
}

- (void)deleteProvisioningProfile:(ALTProvisioningProfile * _Nonnull)provisioningProfile
                completionHandler:(void (^_Nonnull)(BOOL success, NSError *_Nullable error))completionHandler {
    [[ALTAppleAPI sharedAPI] deleteProvisioningProfile:provisioningProfile
                                               forTeam:self.team
                                               session:self.session
                                     completionHandler:completionHandler];
    
}

- (void)generateProvisionProfileForAppID:(ALTAppID *)appID completionHandler:(void (^)(ALTProvisioningProfile * _Nullable, NSError * _Nullable))completionHandler {
    [self fetchProvisioningProfileForAppID:appID completionHandler:^(ALTProvisioningProfile * _Nullable provisioningProfile, NSError * _Nullable error) {
        if (error) {
            !completionHandler ?: completionHandler(nil, error);
            return;
        }
        [self deleteProvisioningProfile:provisioningProfile completionHandler:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                !completionHandler ?: completionHandler(nil, error);
                return;
            }
            
            [self fetchProvisioningProfileForAppID:appID completionHandler:completionHandler];
        }];
    }];
}

@end
